import { NextResponse } from 'next/server';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
const VALID_MODES = ['HT1','LT1','HT2','LT2','HT3','LT3','HT4','LT4','HT5','LT5'];
const SCORE_VALUES = {
  HT1: 60, LT1: 45, HT2: 30, LT2: 20,
  HT3: 10, LT3: 6, HT4: 4, LT4: 3,
  HT5: 2, LT5: 1
};

export async function POST(req: Request) {
  try {
    const { playerId, mode } = await req.json();
    
    if (!VALID_MODES.includes(mode)) {
      return NextResponse.json(
        { error: 'Invalid game mode' },
        { status: 400 }
      );
    }

    const newScore = await prisma.score.create({
      data: {
        mode,
        value: SCORE_VALUES[mode as keyof typeof SCORE_VALUES],
        playerId: Number(playerId)
      }
    });

    return NextResponse.json(newScore, {
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'POST',
        'Access-Control-Allow-Headers': 'Content-Type'
      }
    });
  } catch (error) {
    return NextResponse.json(
      { error: 'Failed to add score' },
      { status: 500 }
    );
  }
}
